package com.heima.article.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.es.ApArticleSearch;
import com.heima.article.es.ApArticleSearchRepository;
import com.heima.article.service.IApArticleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class ApArticleESInit {
    @Autowired
    private IApArticleService apArticleService;
    @Autowired
    private ApArticleSearchRepository apArticleSearchRepository;

    @Test
    public void initEs() {
        //查询上架的并且没有删除的文章
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticle::getIsDown, false);
        queryWrapper.eq(ApArticle::getIsDelete, false);
        List<ApArticle> articleList = apArticleService.list(queryWrapper);


        List<ApArticleSearch> list = new ArrayList<>();
        for (ApArticle apArticle : articleList) {
            ApArticleSearch articleSearch = new ApArticleSearch();
            BeanUtils.copyProperties(apArticle, articleSearch);
            list.add(articleSearch);
        }
        //收到ES中
        apArticleSearchRepository.saveAll(list);
    }
}
